//
//  HLFoldSectionView.m
//  HLAerospace
//
//  Created by hulian-ios on 17/9/7.
//  Copyright © 2017年 hulian-LB. All rights reserved.
//

#import "HLFoldSectionView.h"
#import <Masonry/Masonry.h>
#import "HLFoldList.h"
#import "HLSubSystem.h"

@interface HLFoldSectionView()

/** 文字  */
@property (nonatomic ,strong) UILabel *title;

@end

@implementation HLFoldSectionView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        [self setUI];
    }
    return self;
}

- (UIImageView *)arrowView
{
    if (!_arrowView ) {
        _arrowView = [[UIImageView alloc] init];
        _arrowView.image = [UIImage imageNamed:@"home_course_fold_arrow"];
    }
    return _arrowView;
}

- (UILabel *)title
{
    if (!_title ) {
        _title = [[UILabel alloc] init];
        _title.backgroundColor = [UIColor whiteColor];
        _title.textColor = HLRGB(60, 60, 60);
        _title.font = [UIFont systemFontOfSize:IS_IPHONE_5 ? 12: kMainTitleFontSize];
    }
    return _title;
}


- (void)setUI
{
    [self addSubview:self.title];
    [self addSubview:self.arrowView];
    
    self.backgroundColor = [UIColor whiteColor];
    self.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
    self.isUnFold = NO;
    [self addGestureRecognizer:tap];
}


- (void)layoutSubviews
{
    [super layoutSubviews];

    [self.title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.left.equalTo(self).offset(10);
        make.width.equalTo(@(self.width *0.7));
        make.height.equalTo(@(30));
    }];
    
    CGFloat arrowW = IS_IPHONE_5 ? 7 : 9;
    CGFloat arrowH = IS_IPHONE_5 ? 11: 14;
    [self.arrowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY);
        make.right.equalTo(self.mas_right).offset(-10);
        make.width.equalTo(@(arrowW));
        make.height.equalTo(@(arrowH));
    }];
}

- (void)setFoldList:(HLFoldList *)foldList
{
    _foldList = foldList;
    self.title.text = foldList.list_name;
}

- (void)setSubSystem:(HLSubSystem *)subSystem
{
    _subSystem = subSystem;
    self.title.text = subSystem.subsystem_name;
}

- (void)setIsUnFold:(BOOL)isUnFold
{
    _isUnFold = isUnFold;
    
    /**** 箭头转向 ****/
    [UIView animateWithDuration:0.25 animations:^{
        self.arrowView.transform =  isUnFold ?  CGAffineTransformMakeRotation(0.5 * M_PI) : CGAffineTransformIdentity;
    }];
}

- (void)tap:(UITapGestureRecognizer *)recognizer
{
    if ([_delegate respondsToSelector:@selector(foldSectionView:didSelectedWith:)]) {
        [_delegate foldSectionView:self didSelectedWith:self.foldList];
    }
    
  
}

@end
